package cn.jxwazx.sheinspringboot.controller;

import cn.jxwazx.sheinspringboot.entity.BaseColumn;
import cn.jxwazx.sheinspringboot.entity.BaseNameValue;
import cn.jxwazx.sheinspringboot.entity.R;
import cn.jxwazx.sheinspringboot.entity.param.ProductBatchSaveOrUpdateParam;
import cn.jxwazx.sheinspringboot.entity.param.ProductPageParam;
import cn.jxwazx.sheinspringboot.entity.po.ProductList;
import cn.jxwazx.sheinspringboot.service.ProductListService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2024年03月14日 11:25
 * @description believe in yourself
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class ProductListController {

    private final ProductListService productListService;

    @PostMapping("/batchSaveOrUpdate")
    public Object batchSaveOrUpdate(@RequestBody List<ProductBatchSaveOrUpdateParam> params) {
        productListService.batchSaveOrUpdate(params);
        return R.success();
    }

    @PostMapping("/page")
    public Object page(@RequestBody ProductPageParam param) {
        Page<ProductList> page = productListService.filterProduct(param)
                .page(param.getPage());
        return R.successPage(page);
    }

    @GetMapping("/getSyncDetail")
    public Object getSyncDetail() {
        return productListService.lambdaQuery().isNull(ProductList::getType5).last("limit 1").one().getProductUrl();
    }

    @GetMapping("/getRandSyncDetail")
    public Object getRandSyncDetail() {
        List<ProductList> list = productListService.lambdaQuery()
                .select(ProductList::getProductUrl, BaseColumn::getId)
                .isNull(ProductList::getShopName)
//                .or()
//                .isNull(ProductList::getType_5)
//                .or()
//                .eq(ProductList::getType_5, "")
                .or()
                .isNull(ProductList::getType1)
                .or()
                .eq(ProductList::getType1, "")

                .orderByAsc(ProductList::getSyncTime)

//                .or()
//                .eq(ProductList::getShopName, "")
//                .or()
//                .eq(ProductList::getStartText, "0")
//                .ne(ProductList::getAllCommentCount, "0")
                .last("limit 500")
                .list();

//        List<ProductList> list = productListService.lambdaQuery()
//                .select(ProductList::getProductUrl)
//                .and(r->{
//                    r.ne(ProductList::getNearSaleCount, "0").or().ne(ProductList::getRank, "");
//                })
//
//                .and(r -> {
//                    r.eq(ProductList::getShopName, "").or().isNull(ProductList::getShopName);
//                })
//                .last("limit 500")
//                .list();

        if (CollUtil.isEmpty(list)) {
            list = productListService.lambdaQuery().select(ProductList::getProductUrl)
                    .orderByDesc(ProductList::getLastUpdateTime).last("limit 100").list();
        }
        ProductList productList = list.get(RandomUtil.randomInt(0, list.size()));
        productListService.lambdaUpdate().eq(BaseColumn::getId, productList.getId())
                .set(ProductList::getSyncTime, LocalDateTime.now())
                .update();
        return productList.getProductUrl();
    }

    /**
     * 排名列表
     *
     * @param param
     * @return
     */
    @PostMapping("/rankPage")
    public Object rankPage(@RequestBody ProductPageParam param) {

        Page<ProductList> page = productListService.lambdaQuery()
                .ne(ProductList::getRank, "")
                .page(param.getPage());
        return R.successPage(page);
    }

    @PostMapping("/productDown")
    public Object productDown(@RequestBody ProductList param) {
        ProductList one = productListService.lambdaQuery().eq(ProductList::getProductUrl, param.getProductUrl())
                .one();
        param.setId(one.getId());
        productListService.updateById(one);
        return R.success();
    }

    /**
     * 排名列表
     *
     * @param param
     * @return
     */
    @PostMapping("/rankList")
    public Object rankList(@RequestBody ProductPageParam param) {
        List<ProductList> page = productListService.filterProduct(param)
                .ne(ProductList::getRank, "")
                .list();
        return R.success(page);
    }


    @GetMapping("/typeList/{type}")
    public Object typeList(@PathVariable("type") Integer type) {
        List<BaseNameValue<String, String>> typeList = productListService.getTypeList(type);
        return R.success(typeList);
    }

    @GetMapping("/rankSyncList")
    public Object rankSyncList() {
        List<ProductList> list = productListService
                .lambdaQuery()
                .select(ProductList::getProductUrl)
                .ne(ProductList::getRank, "")
                //.isNull(ProductList::)
                .eq(ProductList::getNearSaleCount, 0)
                .last("limit 500")
                .list();
        for (ProductList productList : list) {
            productList.setProductUrl(productList.getProductUrl().replace("www.shein", "m.shein"));
        }
        return list.get(RandomUtil.randomInt(0, list.size())).getProductUrl();
    }

    @PostMapping("/exportData")
    public Object exportData(@RequestBody ProductPageParam param) {
        String url = productListService.exportData(param);
        return R.success(url);
    }


    @PostMapping("/fix")
    public Object fix() {
        productListService.fix();
        return R.success();
    }

}
